package com.xcc.dao;

import com.xcc.eneity.Role;
import org.springframework.jdbc.core.PreparedStatementCreator;
import org.springframework.jdbc.core.support.JdbcDaoSupport;
import org.springframework.jdbc.support.GeneratedKeyHolder;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

/**
 * @create: 2019-07-08 19:02
 * @author: Aner
 * @description:
 **/
public class RoleDaoImpl extends JdbcDaoSupport implements RoleDao{

    @Override
    public Role createRole(final Role role) {
        final String sql="insert into sys_roles(role,description,available) values(?,?,?)";

        GeneratedKeyHolder keyHolder =new GeneratedKeyHolder();
        getJdbcTemplate().update(new PreparedStatementCreator() {
            @Override
            public PreparedStatement createPreparedStatement(Connection connection) throws SQLException {
                PreparedStatement psst =connection.prepareStatement(sql,new String[]{"id"});
                psst.setString(1,role.getRole());
                psst.setString(2,role.getDescription());
                psst.setBoolean(3,role.getAvailable());
                return psst;
            }
        },keyHolder);
        role.setId(keyHolder.getKey().longValue());
        return role;
    }

    @Override
    public void deleteRole(Long roleId) {
        //首先把role关联的相关表数据删掉
        String sql="delete from sys_users_roles where role_id=?";
        getJdbcTemplate().update(sql,roleId);

        sql="delete from sys_roless where id=?";
        getJdbcTemplate().update(sql,roleId);
    }

    @Override
    public void correlationPermissions(Long roleId, Long... permissionIds){
        if (permissionIds == null || permissionIds.length==0){
            return ;
        }
        String sql="insert into sys_roles_permissions (role_id,permission_id) values(?,?)";
        for (Long permissionId : permissionIds){
            if (!exists(roleId,permissionId)){
                getJdbcTemplate().update(sql,roleId,permissionId);
            }
        }
    }

    @Override
    public void uncorrelatioinPermissions(Long roleId, Long... permissionIds) {
        if (permissionIds ==null || permissionIds.length==0){
            return ;
        }
        String sql="delete from sys_roles_permissions where role_id=? and permission_id=?";
        for(Long permissionId: permissionIds){
            if (exists(roleId,permissionId)){
                getJdbcTemplate().update(sql,roleId,permissionId);
            }
        }
    }


    private boolean exists(Long roleId, Long... permissionId){
        String sql="select count(1) from sys_roles_permissions where role_id=? and permission_id=?";
        return getJdbcTemplate().queryForObject(sql,Integer.class,roleId,permissionId)!=0;
    }
}
